/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.nico.db.sql.editor;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple object to represent a postgis function as seen by autocompletion:
 * Name, return type, list of parameters
 * @author nicolas
 */
public class PostgisFunction {
    private String name;
    private String returnType;
    private List<PostgisFunctionParam> params;
    private String htmlDoc;
    
    public PostgisFunction(String name, String returnType, List<PostgisFunctionParam> params, String htmlDoc) {
        this(name);
        setHtmlDoc(htmlDoc);
        setReturnType(returnType);
        setParams(params);
    }

    public PostgisFunction(String name) {
        this.name = name;
        this.htmlDoc  = "index.html";
        this.returnType = "";
    }

    public String getHtmlDoc() {
        return htmlDoc;
    }
    
    
    
    public String getName() {
        return name;
    }
    
    /** Returns name and params 
    public String getFullName() {
        String res = getName() + "(";
        
        if (this.getParams() != null && ! this.getParams().isEmpty()) {
            String sep = "";
            for (PostgisFunctionParam p : params) {
                res += sep + p.getType() + " " + p.getName();
                sep = ", ";
            }
        }
        res += ")";
        return res;
    }
    * 
    * 
*/
    
    public final void setHtmlDoc(String doc) {
        this.htmlDoc = doc == null ? "index.html" : doc;
    }
    public void setName(String name) {
        this.name = name == null ? "" : name;
    }

    public String getReturnType() {
        return returnType;
    }

    public final void setReturnType(String returnType) {
        this.returnType = returnType == null ? "" : returnType;
    }

    public List<PostgisFunctionParam> getParams() {
        return params;
    }

    public final void setParams(List<PostgisFunctionParam> params) {
        this.params = params;
    }
    
    /**
     * 
     * @return The java code suitable to add this function to the default function map (see PostgisMetadata class)
     */
    public String toJavaCode() {
// this.curPgObj.functionsDocMap.put("ST_3DDistance".toLowerCase(), PostgisFunction.toList(new PostgisFunction("ST_3DDistance"))); //NOI18N
// this.curPgObj.functionsDocMap.put("box2d_type".toLowerCase(), PostgisFunction.toList(new PostgisFunction("box2d_type", "GEOM", ))); //NOI18N
        StringBuilder b = new StringBuilder();
        String paramList = "null";
        
        if (this.params != null && this.params.size() > 0) {
            //Arrays.asList(new PostgisFunctionParam("", ""), new PostgisFunctionParam("", ""))
            paramList = "Arrays.asList(";
            String sep = "";
            for (PostgisFunctionParam p : params) {
                paramList += sep + "new PostgisFunctionParam(\"" + p.getName().replace("\"", "") + "\", \"" + p.getType() + "\")";
                sep = ", ";
            }
            paramList += ")";
            
        }
        b.append("res.put(\"").append(this.getName().toLowerCase())
                .append("\", PostgisFunction.toList(new PostgisFunction(\"").append(this.getName())
                .append("\", \"").append(this.getReturnType())
                .append("\", ").append(paramList).append(", \"")
                .append(this.getHtmlDoc()).append("\"))); //NOI18N");
        return b.toString();
    }
    
    
    /**
     * helper returning a list of one element from given PostgisFunction
     */
    public static List<PostgisFunction> toList(PostgisFunction pf) {
        ArrayList<PostgisFunction> res = new ArrayList<PostgisFunction>();
        res.add(pf);
        return res;
    }
}
